package l1j.opqlo.NewSystem.地圖限時;

import l1j.server.server.model.L1ItemDelay;
import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;
import static l1j.server.server.model.skill.L1SkillId.MapTimer;

public class MapTimeControl {

	private final int _time;
	private final int _levelmet;
	private final int _now_level;
	private final int _max_level;
	private final int _locx;
	private final int _locy;
	private final short _map;
	private final boolean _removeitem;

	public MapTimeControl(final short map, final int locx, final int locy,
			final int time, final int levelmet, final int now_level,
			final int max_level, final int removeitem) {

		this._locx = locx;
		this._locy = locy;
		this._map = map;
		this._time = time;
		this._levelmet = levelmet;
		this._now_level = now_level;
		this._max_level = max_level;
		this._removeitem = removeitem != 0 ? true : false;

	}

	public void TelePort(final L1PcInstance pc, final L1ItemInstance item) {

		final String msg;
		if(pc.isInHell()){
			pc.sendPackets(new S_ServerMessage(647));
			return;
		}
		final boolean NotGM = !pc.isGm(); 
		if (NotGM && this._levelmet > 0 && pc.get_metempsychosis() < this._levelmet) {

			msg = "地圖限制 [" + this._levelmet + "轉] 以上才能進入。";

		} else if (NotGM && this._now_level > 0 && pc.getLevel() < this._now_level) {

			msg = "地圖限制 [" + this._now_level + "級] 以上才能進入。";

		} else if (NotGM && this._max_level > 0 && pc.getLevel() >= this._max_level) {

			msg = "地圖限制 超過[" + this._max_level + "級] 不可進入。";

		} else {
			if (this._time > 0) {
				msg = "你可以在這停留 [" + this._time + "] 分鐘";
				pc.killSkillEffectTimer(MapTimer);
				pc.setSkillEffect(MapTimer, this._time * 60000);

			} else {
				msg = null;
			}

			if (this._removeitem) {
				pc.getInventory().removeItem(item, 1);
			}
			L1Teleport.teleport(pc, this._locx, this._locy, this._map, 0, true);
			pc.TelePortSetSkill = true;
		}

		if (msg != null) {
			pc.sendPackets(new S_SystemMessage(msg));
		}
		L1ItemDelay.teleportUnlock(pc, item);
	}
}
